package databaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;






import util.IPUtils;
import util.TimeUtils;
import bean.UserBean;

public class LoginDao {

	public UserBean login(String user, String password) throws Exception {
		Connection con = DataAccess.getConnection();
		String sql = "select * from users where userName=? and password=?";

		PreparedStatement pst = null;
		ResultSet rs = null;
		UserBean ub = null;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, user);
			pst.setString(2, password);
			rs = pst.executeQuery();

			if (rs.next()) {
				ub = new UserBean();
				ub.setUserID(rs.getString("userID"));
				ub.setNickName(rs.getString("nickName"));
				ub.setUserName(rs.getString("userName"));
				ub.setPassword(rs.getString("password"));
				ub.setUserDescription(rs.getString("userDescription"));
				ub.setRoleID(rs.getString("roleID"));
				ub.setUserCreateTime(rs.getString("userCreateTime"));
				ub.setLoginTimeLong(rs.getString("loginTimeLong"));
				ub.setLoginTime(rs.getString("loginTime"));
				ub.setCurrentStatus(rs.getString("currentStatus"));
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(rs!=null){
					rs.close();
				}
				if(pst!=null){
					pst.close();
				}
				if(con!=null){
					con.close();
				}
			}catch (Exception e2){
				e2.printStackTrace();
			}
		}
		
		return ub;
	}
	
	// 登录时更新更新登录情况
	
	public void updateUserDao(UserBean ub) throws ClassNotFoundException{
		
		Connection con = DataAccess.getConnection();
		String sql = "update users set  loginTime=?, currentStatus=?,IP=?  where userID=? ";

		PreparedStatement pst = null;
		ResultSet rs = null;
		
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, TimeUtils.getCurrTime());
			pst.setString(2, "在线");
			pst.setString(3, ub.getIP());
			pst.setString(4, ub.getUserID());
			

			pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				if(rs!=null){
					rs.close();
				}
				if(pst!=null){
					pst.close();
				}
				if(con!=null){
					con.close();
				}
			}catch (Exception e2){
				e2.printStackTrace();
			}
		}
		
		
	}
	
	
	
	
	
	
	// 退出时更新更新登录情况
	
		public void updateUserDao2LoginOut(UserBean ub) throws ClassNotFoundException{
			
			Connection con = DataAccess.getConnection();
			String sql = "update users set  loginTimeLong=?, currentStatus=? where userID=? ";

			PreparedStatement pst = null;
			ResultSet rs = null;
			
			try {
				pst = con.prepareStatement(sql);
//				System.out.println("---"+ub.getLoginTime()+"----"+TimeUtils.getCurrTime());
//				System.out.println(TimeUtils.getMinute(ub.getLoginTime(), TimeUtils.getCurrTime()));
				int timelong = Integer.parseInt(ub.getLoginTimeLong())+Integer.parseInt(TimeUtils.getMinute(ub.getLoginTime(), TimeUtils.getCurrTime()));
				pst.setString(1, String.valueOf(timelong));
				pst.setString(2, "离线");
				pst.setString(3, ub.getUserID());
				

				pst.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try{
					if(rs!=null){
						rs.close();
					}
					if(pst!=null){
						pst.close();
					}
					if(con!=null){
						con.close();
					}
				}catch (Exception e2){
					e2.printStackTrace();
				}
			}
			
		}
	
	
	
	
	
	
	
	
}
